package com.ruoyi.security.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.security.domain.AfElevatorRepair;
import com.ruoyi.security.mapper.AfElevatorRepairMapper;
import com.ruoyi.security.service.IAfElevatorRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 电梯维修单Service业务层处理
 *
 * @author cjie
 * @date 2022-07-25
 */
@Service
public class AfElevatorRepairServiceImpl implements IAfElevatorRepairService {
    @Autowired
    private AfElevatorRepairMapper afElevatorRepairMapper;
    
    /**
     * 查询电梯维修单
     *
     * @param orderNo 电梯维修单ID
     * @return 电梯维修单
     */
    @Override
    public AfElevatorRepair selectAfElevatorRepairById(String orderNo) {
        return afElevatorRepairMapper.selectAfElevatorRepairById(orderNo);
    }

    /**
     * 查询电梯维修单列表
     *
     * @param afElevatorRepair 电梯维修单
     * @return 电梯维修单
     */
    @Override
    public List<AfElevatorRepair> selectAfElevatorRepairList(AfElevatorRepair afElevatorRepair) {
        return afElevatorRepairMapper.selectAfElevatorRepairList(afElevatorRepair);
    }

    /**
     * 新增电梯维修单
     *
     * @param afElevatorRepair 电梯维修单
     * @return 结果
     */
    @Override
    public int insertAfElevatorRepair(AfElevatorRepair afElevatorRepair) {
        afElevatorRepair.setOrderNo(IdUtil.getSnowflake(0, 0).nextIdStr());
        afElevatorRepair.setCreateTime(DateUtils.getNowDate());
        afElevatorRepair.setCreateBy(SecurityUtils.getUsername());
        return afElevatorRepairMapper.insertAfElevatorRepair(afElevatorRepair);
    }

    /**
     * 修改电梯维修单
     *
     * @param afElevatorRepair 电梯维修单
     * @return 结果
     */
    @Override
    public int updateAfElevatorRepair(AfElevatorRepair afElevatorRepair) {
        return afElevatorRepairMapper.updateAfElevatorRepair(afElevatorRepair);
    }

    /**
     * 批量删除电梯维修单
     *
     * @param orderNos 需要删除的电梯维修单ID
     * @return 结果
     */
    @Override
    public int deleteAfElevatorRepairByIds(String[] orderNos) {
        return afElevatorRepairMapper.deleteAfElevatorRepairByIds(orderNos);
    }

    /**
     * 删除电梯维修单信息
     *
     * @param orderNo 电梯维修单ID
     * @return 结果
     */
    @Override
    public int deleteAfElevatorRepairById(String orderNo) {
        return afElevatorRepairMapper.deleteAfElevatorRepairById(orderNo);
    }

    @Override
    public Map<String, Integer> selectAfElevatorRepairByMonth(String month) {
        Map<String, Integer> resultMap = new LinkedHashMap<>();
/*        //分组查询list ,order by date asc
        List<AfElevatorRepair> list = afElevatorRepairMapper.selectAfElevatorRepairByMonth(month);
        //list to sorted map
        for(AfElevatorRepair repair:list){
            resultMap.put(repair.getRepairDate(),repair.getCont());
        }*/
        List<String> list = afElevatorRepairMapper.selectElevatorRepairByMonth(month);
        for(String repair:list){
            if (resultMap.containsKey(repair)){
                resultMap.put(repair,resultMap.get(repair)+1);
            }else {
                resultMap.put(repair,1);
            }
        }
        return resultMap;
    }
}
